package com.qnzw.zjmy.volley.httpUtil;

import android.text.TextUtils;

import com.qnzw.zjmy.volley.attribute.AUAttributeUtils;
import com.qnzw.zjmy.volley.httpUtil.interfase.HUCDataAnalysisInterfase;
import com.qnzw.zjmy.volley.state.HUCState;

import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


public final class HUCDataAnalysis extends HUCBase implements
        HUCDataAnalysisInterfase {
    private static HUCDataAnalysis dataAnalysis;
    public static final String CODE = "code";
    public static final String DATALIST = "data";
    //true 对象 ， false列表

    /**
     * 网络数据解析工具类
     *
     * @return
     */
    public static HUCDataAnalysis getInstance() {
        if (dataAnalysis == null) {
            dataAnalysis = new HUCDataAnalysis();
        }
        return dataAnalysis;
    }

    private HUCDataAnalysis() {
        super();
    }

    private Object parseJsonObject(Class<?> class1, JSONObject jsonObject) {
        List<String> attr = AUAttributeUtils.getInstance().getAttributeName(class1,
                false);
        Object obj = null;
        try {
            obj = class1.newInstance();
            for (int j = 0; j < attr.size(); j++) {
                String setName = attr.get(j);
                String setValue = jsonObject.optString(setName);
                setName = "set" + setName.substring(0, 1).toUpperCase()
                        + setName.substring(1);
                Method method = class1.getDeclaredMethod(setName, String.class);
                method.setAccessible(true);
                method.invoke(obj, setValue);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return obj;
    }

    @Override
    public Object parseJsonList(Class<?> class1, String jsonStr, boolean isFlag) {
        if (TextUtils.isEmpty(jsonStr)) {
            return null;
        }
        List<Object> list = new ArrayList<Object>();
        try {
            JSONObject json = new JSONObject(jsonStr);
            int code = json.optInt(CODE);
            String dataList = json.optString(DATALIST);
            switch (code) {
                case HUCRequest.SUCCESS:
                    if (isFlag) {
                        JSONObject jsonObject = new JSONObject(dataList);
//                        String token = jsonObject.optString("token", "");
//                        if (!TextUtils.isEmpty(token)) {
//                            GameApplication.getMyApplication().token = token;
//                            SharedPreferences et = GameApplication.getMyApplication().getSharedPreferences("Token", MODE_PRIVATE);
//                            SharedPreferences.Editor editor = et.edit();
//                            editor.putString("token", token);
//                            editor.commit();
//                        }
                        Object obj = parseJsonObject(class1, jsonObject);
                        return obj;
                    }
                    JSONArray array = new JSONArray(dataList);
                    int arrayLen = array.length();
                    if (array != null || arrayLen > 0) {
                        for (int i = 0; i < arrayLen; i++) {
                            JSONObject jsonObject = array.getJSONObject(i);
                            Object obj = parseJsonObject(class1, jsonObject);
                            list.add(obj);
                        }
                    }
                    break;
                case HUCState.HUC_FAIL_CODE:
                    return dataList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Object parseJsonList(Class<?> class1, String jsonStr) {
        if (TextUtils.isEmpty(jsonStr)) {
            return null;
        }
        List<Object> list = new ArrayList<Object>();
        try {
            JSONArray array = new JSONArray(jsonStr);
            int arrayLen = array.length();
            if (array != null || arrayLen > 0) {
                for (int i = 0; i < arrayLen; i++) {
                    JSONObject jsonObject = array.getJSONObject(i);
                    Object obj = parseJsonObject(class1, jsonObject);
                    list.add(obj);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
